const { app, BrowserWindow, Menu, screen, dialog, globalShortcut, ipcMain } = require('electron');
const menuTemp = require('./menu');

function initDockMenu() {
  var dockMenu = Menu.buildFromTemplate([
    {
      label: 'New Window',
      click: function () {
        console.log('New Window');
      },
    },
  ]);
  app.dock.setMenu(dockMenu);
}
function initApplicationMenu() {
  let menu = Menu.buildFromTemplate(menuTemp);
  Menu.setApplicationMenu(menu);
}
function initMainWindow() {
  mainWindow = new BrowserWindow({
    width: 960,
    height: 760,
    center: true,
    resizable: false,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true,
    },
  });
  mainWindow.on('focus', () => {
    // mac下快捷键失效的问题
    if (process.platform === 'darwin') {
      let contents = mainWindow.webContents;
      globalShortcut.register('CommandOrControl+C', () => {
        contents.copy();
      });
      globalShortcut.register('CommandOrControl+V', () => {
        contents.paste();
      });
      globalShortcut.register('CommandOrControl+X', () => {
        contents.cut();
      });
      globalShortcut.register('CommandOrControl+A', () => {
        contents.selectAll();
      });
    }
  });
  mainWindow.on('blur', () => {
    globalShortcut.unregisterAll(); // 注销键盘事件
  });
  if (process.env.NODE_ENV === 'development') {
    mainWindow.openDevTools();
  }
  url = 'http://localhost:3000';
  mainWindow.loadURL(url);
  // if(process.env.npm_config_isReact){
  //   url = 'http://localhost:3000';
  //   mainWindow.loadURL(url);
  // }
  // else if (process.env.npm_config_isServe) {
  //   url = 'http://localhost:8080';
  //   mainWindow.loadURL(url);
  // } else {
  //   let url = 'http://127.0.0.1:5188';
  //   mainWindow.loadURL(url);
  // }

  ipcMain.on('login-success', () => {
    mainWindow.setSize(1080, 960);
    mainWindow.resizable = true;
    mainWindow.webContents.send('login-success-callback');
  })
}
function initWinow() {
  initMainWindow();
  initApplicationMenu();
  initDockMenu();
}
module.exports = { initWinow };
